package cn.yup.service.impl;

import cn.yup.domain.Department;
import cn.yup.mapper.DepartmentMapper;
import cn.yup.query.QueryObject;
import cn.yup.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service   // 表示把该类创建对象交给Spring 管理  @controller
public class DepartmentServiceImpl implements IDepartmentService {


    @Autowired //  表示 从spring 容器中 获取 DepartmentMapper 对象 赋给 departmentMapper 这个变量
    private DepartmentMapper departmentMapper  ;


    @Override
    public PageInfo findAll(QueryObject queryObject) {
        // PageHelper 插件处理分页
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        // 当前页的数据
       List<Department> departments =  departmentMapper.findAll();

        return  new  PageInfo(departments);
    }

    @Override
    public void deleteById(Long id) {
        departmentMapper.deleteById(id) ;
    }

    @Override
    public void saveOrUpdate(Department department) {
        if (department.getId() != null) {
            departmentMapper.updateById(department);
        }
        else {

            departmentMapper.save(department);
        }
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

}
